//
//  WelcomeViewController.swift
//  swft_weibo
//
//  Created by mac on 15/9/12.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class WelcomeViewController: UIViewController {

    var iconlayout :NSLayoutConstraint?
    override func viewDidLoad() {
        
        
        view.backgroundColor=UIColor.greenColor()
        
        //1.添加子控件
        view.addSubview(bgView!)
        view.addSubview(iconView!)
        view.addSubview(loganTitle!)
        
        //2.设置子控件的布局
        bgView?.xmg_Fill(view)
        //设置icon的约束，然后取得他关于bottom的约束
        let iconlayouts=iconView?.xmg_AlignInner(type: XMG_AlignType.BottomCenter, referView: view, size: nil, offset: CGPointMake(0, -150))
        
        //取出iconview的bottom的约束，用来做动画
        iconlayout=iconView?.xmg_Constraint(iconlayouts!, attribute: NSLayoutAttribute.Bottom)
        
        //设置loganTitle的约束
        loganTitle?.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconView!, size: nil, offset: CGPointMake(0, 20))
        
        print(view.frame)
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
        
    }
    
    func startAnimation()
    {
         print((iconlayout?.constant)!)
        iconlayout?.constant = -(UIScreen.mainScreen().bounds.height+(iconlayout?.constant)!)
         print((iconlayout?.constant)!)
       
        UIView.animateWithDuration(1, animations: { () -> Void in
            
            iconView?.layoutIfNeeded()
            
            }) { (compelete) -> Void in
                
                UIView.animateWithDuration(2, animations: { () -> Void in
                    loganTitle?.alpha=1
                    
                    }, completion: { (complete) -> Void in
                        //切换根控制器
                        NSNotificationCenter.defaultCenter().postNotificationName("convertRootCtroller", object: false)
                })
                
        }
    
    
    }
    
    ///头像图标
    lazy var iconView :UIImageView?={
        
        let iconView=UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius=iconView.frame.size.width*0.5
        iconView.layer.masksToBounds=true
        
        return iconView
    
    }()
    ///设置背景
    lazy var bgView :UIImageView?=UIImageView(image: UIImage(named: "ad_background"))
    
    ///设置标语logan
    lazy var loganTitle :UILabel?={
       
        let label=UILabel()
        label.alpha=0
        
        label.text="欢迎回来"
        return label
    }()
    
}
